<?php
/**
 * Created by Zhy
 * User: Zhy
 * Date: 2023/1/11
 * Time: 15:51
 */

namespace app\admin\controller;

use think\Db;

class Giftbagwithdraw extends Base
{
    /**
     * 礼包提现列表
     */
    public function index(){
        if(request()->isAjax ()){
            $gw_number = trim(input('gw_number'), ' '); // 提现单号
            $user_username = trim(input('user_username')); // 用户名称
            $user_phone = trim(input('user_phone')); // 手机号
            $start_time = input('start_time');
            $end_time = input('end_time');
            $type = input('type'); // 1 支付宝     2 微信

            $where = [];
            if(isset($gw_number) && $gw_number != ''){
                $where['gw_number'] = ['like', '%' . $gw_number . '%'];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($type) && $type != ''){
                $where['gw_type'] = $type;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['gw_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['gw_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['gw_time'] = ['<=', $end_time];
            }

            $Nowpage = input('page') ? input('page') : 1;
            $limits = input("limit") ? input("limit") : 10;
            $count = Db::table('up_bo_giftbag_withdraw')
                ->join('up_u_user', 'gw_user_id = user_id', 'LEFT')
                ->where($where)
                ->count('gw_id');// 获取总条数
            $lists = Db::table('up_bo_giftbag_withdraw')
                ->join('up_u_user', 'gw_user_id = user_id', 'LEFT')
                ->field('gw_id, gw_type, gw_number, gw_price, gw_time, user_username, user_phone, gw_user_code')
                ->where($where)
                ->order('gw_time desc')
                ->page($Nowpage,$limits)
                ->select();

            return json(['code' => 220, 'msg' => '', 'count' => $count, 'data' => $lists]);
        }
        return $this->fetch();
    }

    /**
     * 提现详情
     * @return mixed
     */
    public function gwDetail(){
        $gw_id = input('gw_id');
        $detail = Db::table('up_bo_giftbag_withdraw')
            ->join('up_u_user', 'gw_user_id = user_id', 'LEFT')
            ->field('user_username, user_phone, gw_type, gw_number, gw_price, gw_zh, gw_real_name, gw_openid, 
            gw_time, gw_user_code')
            ->where('gw_id', $gw_id)
            ->find();
        return $this->fetch('', ['detail' => $detail]);
    }

    /**
     * 导出
     * @return \think\response\Json
     */
    public function outExcel(){
        set_time_limit(0);
        $gw_ids = input('ids');

        $where = [];
        if($gw_ids == -1){
            $gw_number = trim(input('gw_number'), ' '); // 提现单号
            $user_username = trim(input('user_username')); // 用户名称
            $user_phone = trim(input('user_phone')); // 手机号
            $start_time = input('start_time');
            $end_time = input('end_time');
            $type = input('type'); // 1 支付宝     2 微信

            if(isset($gw_number) && $gw_number != ''){
                $where['gw_number'] = ['like', '%' . $gw_number . '%'];
            }
            if(isset($user_username) && $user_username != ''){
                $where['user_username'] = ['like', '%' . $user_username . '%'];
            }
            if(isset($user_phone) && $user_phone != ''){
                $where['user_phone'] = ['like', '%' . $user_phone . '%'];
            }
            if(isset($type) && $type != ''){
                $where['gw_type'] = $type;
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time != ''){
                $where['gw_time'] = ['between', [$start_time, $end_time]];
            }
            if(isset($start_time) && $start_time != '' && isset($end_time) && $end_time == ''){
                $where['gw_time'] = ['>=', $start_time];
            }
            if(isset($start_time) && $start_time == '' && isset($end_time) && $end_time != ''){
                $where['gw_time'] = ['<=', $end_time];
            }
        }else{
            if($gw_ids == '') return json(['code' => 100, 'msg' => '请选择需要导出的数据']);
            $where['gw_id'] = ['IN', $gw_ids];
        }

        $lists = Db::table('up_bo_giftbag_withdraw')
            ->join('up_u_user', 'gw_user_id = user_id', 'LEFT')
            ->field('gw_id, gw_type, gw_number, gw_price, gw_time, user_username, user_phone, gw_zh, 
            gw_real_name, gw_user_code')
            ->where($where)
            ->order('gw_time desc')
            ->select();

        foreach($lists as $k => $v){
            // 提现方式
            $lists[$k]['gw_type'] = $v['gw_type'] == 1 ? '支付宝' : '微信';
            // 价格
            $lists[$k]['gw_price'] = "\t" . $v['gw_price'] . "\t";
            // 手机号
            $lists[$k]['user_phone'] = "\t" . $v['user_phone'] . "\t";
        }
        $cellname = [
            ['user_username', '提现用户', 12, 'LEFT'],
            ['user_phone', '手机号', 15, 'LEFT'],
            ['gw_number', '提现单号', 25, 'LEFT'],
            ['gw_price', '提现金额', 20, 'LEFT'],
            ['gw_type', '提现方式', 20, 'LEFT'],
            ['gw_real_name', '真实姓名', 20, 'LEFT'],
            ['gw_user_code', '身份证号', 20, 'LEFT'],
            ['gw_zh', '提现账号', 20, 'LEFT'],
            ['gw_time', '提现时间', 20, 'LEFT']
        ];
        $res = exportExcelZhy('提现', 'Giftbag_withdraw', $cellname, $lists);
        return json($res);
    }
}